package com.question.service.impl;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.question.dao.TopicDao;
import com.question.dao.daoFactory.DaoFactory;
import com.question.domain.Topic;
import com.question.exception.Exce;
import com.question.service.TopicService;
import com.question.service.serviceFactory.ServiceFactory;
import com.question.utils.IntegerTools;
import com.question.utils.MyProperties;

public class TopicServiceImpl implements TopicService {
	private TopicDao topicDao = DaoFactory.getInstance().geTopicDao();
	
	//用于获取配置文件的Properties
	MyProperties properties = new MyProperties();
	
	//获取所有题目
	Map<Integer, Topic> map_topic = null;
	
	//存储所有题目信息
	Map<String, Integer> map_infor = null;
	

	/**
	 * 在获取时获取所需数据
	 */
	public TopicServiceImpl(){
		List<Topic> list = topicDao.getTopic();
		map_topic = list.stream().collect(Collectors.toMap(Topic::getQuestionId, topic->topic));
		map_infor = properties.getMostMap();
	}

	/**
	 * 加上第index题的分数，
	 * 
	 * @param index
	 */
	private int addGrade(int index,String answer) {
		if(map_topic.get(index)==null) throw new RuntimeException("题目序号越界");
		if (isRight(index,answer)==0) 
			return 0;
		else {	
			switch (((Topic)map_topic.get(index)).getType()) {
			case 0:
				return map_infor.get("singleGrade");
			case 1:
				return map_infor.get("multipleGrade");
			case 2:
				return map_infor.get("TFGrade");
			default:
				throw new RuntimeException("该题目未找到类型");
			}
		}
	}
	
	/**
	 * 判断第index是否做对,有可能是多选，但是选不全，过给一点分，
	 * 
	 * @param index
	 * @return 返回该同学在该题获得的分数的比重，比如返回5，则该题为5/10=0.5的比重
	 */
	private int isRight(int index,String answer) {
		if(map_topic.get(index)==null) return 0;
		if(index>map_infor.get("totalTopic"))
			throw new RuntimeException("题号错误，越界");
		if(answer.equals(((Topic)(map_topic.get(index))).getAnswer()))
			return 10;
		return 0;
	}

	@Override
	public Topic getTopicByIndex(int index)  throws Exce{
		
		return topicDao.getATopic(index);
	}

	/* (non-Javadoc)
	 * @see com.question.service.TopicService#getGrade()
	 */
	public int getGrade(Map<Integer, String> map) {
		int grade = 0;
		for(Integer key : map.keySet()){
			grade+=addGrade(key,map.get(key));
		}
		return grade;
	}

	@Override
	public List getTopic(Map topicId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int[] getTopicNumber() {
		int[] single = IntegerTools.getDifferentNum(map_infor.get("singleTopic")
				, map_infor.get("minSingle"),
				map_infor.get("maxSingle"));
		int[] multiple = IntegerTools.getDifferentNum(map_infor.get("multipleTopic")
				, map_infor.get("minMultiple"),
				map_infor.get("maxMultiple"));
	/*	int[] tf = IntegerTools.getDifferentNum(map_infor.get("TFTopic")
				, map_infor.get("minTF"),
				map_infor.get("maxTF"));*/
		int[] arr = IntegerTools.connectIntArr(single, multiple);
	//	arr = IntegerTools.connectIntArr(arr, tf);
		return arr;
	}

	public static void main(String[] args) {
		TopicService topicService = ServiceFactory.getInstance().geTopicService();
		/*String str = "1A2B3C4D5G6D7G8Y9E10Q11A12D13A14A15A16D17X18V19W20R";
		Map<Integer, String> map = StringTools.splitAnswer(str);
		System.out.println(topicService.getGrade(map));*/
		int[] arr = topicService.getTopicNumber();
		for(int i=0;i<arr.length;i++)
			System.out.println(arr[i]);
	}

}
